package cn.iocoder.yudao.module.train.dal.mysql.courseclasses;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.course.CourseDO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursestudentregistform.CourseStudentRegistFormDO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.studentcoursescore.StudentCourseScoreDO;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.module.train.dal.dataobject.venue.VenueDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.*;

/**
 * 班级 Mapper
 *
 * @author waynelam
 */
@Mapper
public interface CourseClassesMapper extends BaseMapperX<CourseClassesDO> {

    default PageResult<CourseClassesCommodityDTO> selectPage(CourseClassesPageReqVO reqVO) {
        Page<CourseClassesCommodityDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        MPJLambdaWrapper<CourseClassesDO> wrapper = new MPJLambdaWrapper<CourseClassesDO>()
                .selectAll(CourseClassesDO.class)
                .selectAs(CourseDO::getName, CourseClassesCommodityDTO::getCourseName)
                .select(TeacherDO::getTeacherName)
                .select(CourseCommodityDO::getCommodityName)
                .select(CourseCommodityDO::getBeginTime)
                .select(CourseCommodityDO::getEndTime)
                .eq(CourseCommodityDO::getVipFlag, 0)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                .selectSub(CourseStudentRegistFormDO.class,
                        w -> w
                                .selectCount(CourseStudentRegistFormDO::getId)
                                .eq(CourseStudentRegistFormDO::getClassesId, CourseClassesDO::getId),
                        CourseClassesCommodityDTO::getStudentQuantity)
                .eqIfExists(CourseClassesCommodityDTO::getClassNumber, reqVO.getClassNumber())
                .likeIfExists(CourseDO::getName, reqVO.getCourseName())
                .likeIfExists(CourseCommodityDO::getCommodityName, reqVO.getCommodityName())
                .likeIfExists(TeacherDO::getTeacherName, reqVO.getTeacherName());
        IPage<CourseClassesCommodityDTO> resultPage = selectJoinPage(page, CourseClassesCommodityDTO.class, wrapper);
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

    default CourseClassesCommodityDTO selectById(Long id) {
        return selectJoinOne(CourseClassesCommodityDTO.class,
                new MPJLambdaWrapper<CourseClassesDO>()
                        .selectAll(CourseClassesDO.class)
                        .selectAs(CourseDO::getName, CourseClassesCommodityDTO::getCourseName)
                        .select(TeacherDO::getTeacherName)
                        .select(CourseCommodityDO::getCommodityName)
                        .select(CourseCommodityDO::getBeginTime)
                        .select(CourseCommodityDO::getEndTime)
                        .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                        .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                        .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                        .eq(CourseClassesDO::getId, id));
    }

    default Integer selectClassNumber(Long commodityId) {
        return selectJoinOne(Integer.class, new MPJLambdaWrapper<CourseClassesDO>()
                .selectMax(CourseClassesDO::getClassNumber)
                .eq(CourseClassesDO::getCommodityId, commodityId));
    }

    default List<CourseClassesCommodityDTO> selectList(CourseClassesPageReqVO reqVO) {
        MPJLambdaWrapper<CourseClassesDO> wrapper = new MPJLambdaWrapper<CourseClassesDO>()
                .selectAll(CourseClassesDO.class)
                .selectAs(CourseDO::getName, CourseClassesCommodityDTO::getCourseName)
                .select(TeacherDO::getTeacherName)
                .select(CourseCommodityDO::getCommodityName)
                .select(CourseCommodityDO::getBeginTime)
                .select(CourseCommodityDO::getEndTime)
                .eq(CourseCommodityDO::getVipFlag, 0)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, CourseClassesDO::getCommodityId)
                .leftJoin(CourseDO.class, CourseDO::getId, CourseCommodityDO::getCourseId)
                .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                .eqIfExists(CourseClassesCommodityDTO::getClassNumber, reqVO.getClassNumber())
                .likeIfExists(CourseDO::getName, reqVO.getCourseName())
                .likeIfExists(CourseCommodityDO::getCommodityName, reqVO.getCommodityName())
                .likeIfExists(TeacherDO::getTeacherName, reqVO.getTeacherName());

        return selectJoinList(CourseClassesCommodityDTO.class, wrapper);
    }

}
